package com.fingard.dsp.bank.directbank.ceb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBatchOpenBill;
import com.fingard.dsp.bank.directbank.pab02.Util.api.utils.StringUtil;
import com.fingard.dsp.bank.reqrespobj.batchbill.*;
import com.fingard.dsp.bank.reqrespobj.batchbill.detail.*;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by F on 2019/10/21.
 */
public class CEB01BatchOpenBill extends CEB01Base implements IBatchOpenBill {

    @Override
    public RetAtsBatchQry batchQry(ReqBatchQry query) throws Exception {

        RetAtsBatchQry retAts = new RetAtsBatchQry(query.reqHeader);
        retAts.respCode = "0";
        retAts.writeBefore();
        query.readRecord();
        int start = 1;
        int total = 0;
        try {
            do {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                CEB01Header header = new CEB01Header();
                header.batchID = getBatchID();
                header.TransCode = "b2e003001";
                header.JnlDateTime = new Date();
                writeCommonHeader_2(xmlWriter, header);
                xmlWriter.writeStartElement("TransContent");
                xmlWriter.writeStartElement("ReqData");
                //          ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
                xmlWriter.writeElementString("ClientPatchID", query.reqHeader.transSeqID);
                //            AcNo	账号	是	30	String	支持授权账户，授权关系由票据系统维护，如上送户名则银企通判别户名是否正确
                xmlWriter.writeElementString("AcNo", query.detail.actNum);
                xmlWriter.writeElementString("Cecmno", query.detail.protocolNo);
                String billType;
                if ("1".equals(query.detail.billType)) {
                    billType = "AC01";
                } else {
                    billType = "AC02";
                }
                //            BillType	票据类型	否	4	String	见数据字典
                xmlWriter.writeElementString("BillType", billType);
                //            CDStatus	批次状态	是	4	String	见数据字典
                xmlWriter.writeElementString("BatchStatus", query.detail.batchStatus);
                //协议编号
                //            StartDate	承兑日起始日期	否	8	String	YYYYMMDD
                xmlWriter.writeElementString("StartDate", query.detail.applyStartDate);
                //            EndDate	承兑日终止日期	否	8	String	YYYYMMDD
                xmlWriter.writeElementString("EndDate", query.detail.applyEndDate);
                //            StartNum	起始笔数	是	7	String
                xmlWriter.writeElementString("StartNum", "" + start);
                //            QueryNum	查询笔数	是	7	String
                xmlWriter.writeElementString("QueryNum", "50");
                xmlWriter.writeEndDocument();
                String[] result = sendToBank_2(xmlWriter.toXmlString(), header.TransCode);
                if (result[0].length() == 0) {
                    StringBuilder tmpSbmsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbmsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                        String returnInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                        String returnNum = tmpBkReader.getFirstText("/Transaction/TransContent/TotalNum");
                        if ("0000".equals(returnCode)) {
                            if (total == 0) {
                                total = Integer.valueOf(returnNum);
                            }
                            while (tmpBkReader.read("/Transaction/TransContent/BatchRespList/RespData")) {
                                start++;
                                RetAtsBatchQryDetail detail = new RetAtsBatchQryDetail();
                                //                            BillNo	批次号	是	30	String
                                detail.batchNo = tmpBkReader.readedXElement.getSingleText("BatchNo");
                                //                            RemiCust	出票人	是	50	String
                                detail.drawName = tmpBkReader.readedXElement.getSingleText("Remi");
                                //                            RemiAcct	出票人账号	是	30	String
                                detail.drawAct = tmpBkReader.readedXElement.getSingleText("AcptAcct");
                                //                            RemiBank	出票人开户行	是	50	String
                                detail.drawBnkName = tmpBkReader.readedXElement.getSingleText("AcptBank");
                                //  BillType	票据种类	是	4	String	AC01-银承 AC02-商承
                                detail.billType = "AC01".equals(tmpBkReader.readedXElement.getSingleText("BillType")) ? "1" : "2";
                                //0-光大银行 1-非光大银行
                                detail.bankType = tmpBkReader.readedXElement.getSingleText("BankType");
                                detail.accpBrch = tmpBkReader.readedXElement.getSingleText("AcptBrch");
                                detail.accpBranch = tmpBkReader.readedXElement.getSingleText("AcptBranch");
                                detail.accpBnkCode = tmpBkReader.readedXElement.getSingleText("AcptBankNo");
                                detail.accpAccount = tmpBkReader.readedXElement.getSingleText("AcptAccoun");
                                detail.accpName = tmpBkReader.readedXElement.getSingleText("Acpt");
                                detail.batchStatus = tmpBkReader.readedXElement.getSingleText("BatchStatus");
                                detail.transFlag = "EM00".equals(tmpBkReader.readedXElement.getSingleText("Isendo")) ? "1" : "0";
                                detail.billNum = tmpBkReader.readedXElement.getSingleText("BillNum");
                                detail.batchMoney = tmpBkReader.readedXElement.getSingleText("BatchMoney");
                                detail.protocolNo = tmpBkReader.readedXElement.getSingleText("CecmNo");
                                detail.poundage = tmpBkReader.readedXElement.getSingleText("AcptSumPoundage");
                                detail.reserved1 = tmpBkReader.readedXElement.getSingleText("respond1");
                                detail.reserved2 = tmpBkReader.readedXElement.getSingleText("respond2");

                                retAts.writeDetail(detail);
                            }
                        } else {
                            if (StringUtil.isNull(returnInfo)){
                                returnInfo=tmpBkReader.getFirstText("/transaction/ReturnMsg");
                            }
                            throw new Exception(returnInfo);
                        }

                    } else {
                        throw new Exception(tmpSbmsg.toString());
                    }
                } else {
                    throw new Exception(result[1]);
                }
            } while (start - 1 < total);
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsBatchQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsBatchInfoQry batchInfoQry(ReqBatchInfoQry query) throws Exception {
        RetAtsBatchInfoQry retAts = new RetAtsBatchInfoQry(query.reqHeader);
        retAts.respCode = "0";
        retAts.writeBefore();
        query.readRecord();
        int start = 1;
        int total = 0;
        try {
            do {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                CEB01Header header = new CEB01Header();
                header.batchID = getBatchID();
                header.TransCode = "b2e003002";
                header.JnlDateTime = new Date();
                writeCommonHeader_2(xmlWriter, header);
                xmlWriter.writeStartElement("TransContent");
                xmlWriter.writeStartElement("ReqData");
                //          ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
                xmlWriter.writeElementString("ClientPatchID", query.reqHeader.transSeqID);
                //            AcNo	账号	是	30	String	支持授权账户，授权关系由票据系统维护，如上送户名则银企通判别户名是否正确
                xmlWriter.writeElementString("AcNo", query.detail.actNum);
                String billType;
                if ("1".equals(query.detail.billType)) {
                    billType = "AC01";
                } else {
                    billType = "AC02";
                }
                //            BillType	票据类型	否	4	String	见数据字典
                xmlWriter.writeElementString("BillType", billType);
                xmlWriter.writeElementString("BatchNo", query.detail.batchNo);
                //            StartDate	承兑日起始日期	否	8	String	YYYYMMDD
                xmlWriter.writeElementString("StartDate", query.detail.applyStartDate);
                //            EndDate	承兑日终止日期	否	8	String	YYYYMMDD
                xmlWriter.writeElementString("EndDate", query.detail.applyEndDate);
                //            StartNum	起始笔数	是	7	String
                xmlWriter.writeElementString("StartNum", "" + start);
                //            QueryNum	查询笔数	是	7	String
                xmlWriter.writeElementString("QueryNum", "50");
                xmlWriter.writeEndDocument();

                String[] result = sendToBank_2(xmlWriter.toXmlString(), header.TransCode);

                if (result[0].length() == 0) {
                    StringBuilder tmpSbmsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbmsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                        String returnInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                        String returnNum = tmpBkReader.getFirstText("/Transaction/TransContent/TotalNum");
                        if ("0000".equals(returnCode)) {
                            if (total == 0) {
                                total = Integer.valueOf(returnNum);
                            }
                            while (tmpBkReader.read("/Transaction/TransContent/BatchRespList/RespData")) {
                                start++;
                                RetAtsBatchInfoQryDetail detail = new RetAtsBatchInfoQryDetail();
                                // BillId	票据ID	是	20	String
                                detail.billId = tmpBkReader.readedXElement.getSingleText("BillId");
//                            BillNo	人行票号	是	30	String
                                detail.billNo = tmpBkReader.readedXElement.getSingleText("BillNo");
                                //票据编码
                                detail.billCecmNo = tmpBkReader.readedXElement.getSingleText("BillCecmNo");
//                            BillType	票据种类	是	4	String	AC01-银承 AC02-商承
                                detail.billType = "AC01".equals(tmpBkReader.readedXElement.getSingleText("BillType")) ? "1" : "2";
//                            DueDt	到期日	是	10	String
                                detail.dueDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleText("DueDt"));
//                            RemiDate	出票日	否	10	String	出票未完成时为空
                                detail.issDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleText("RemiDate"));
                                //承兑日
                                detail.accpDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleText("AcptDt"));
//                            BillMoney	票面金额	是	25	String
                                detail.billAmt = tmpBkReader.readedXElement.getSingleText("BillMoney");
//                            RemiCust	出票人	是	50	String
                                detail.drawName = tmpBkReader.readedXElement.getSingleText("RemiCust");
//                            RemiAcct	出票人账号	是	30	String
                                detail.drawAct = tmpBkReader.readedXElement.getSingleText("RemiAcct");
//                            RemiBank	出票人开户行	是	50	String
                                detail.drawBnkName = tmpBkReader.readedXElement.getSingleText("RemiBank");
//                            RemiBankNo	出票人开户行行号	是	20	String
                                detail.drawBnkCode = tmpBkReader.readedXElement.getSingleText("RemiBankNo");
//                            Payee	收款人名称	是	50	String
                                detail.payeeName = tmpBkReader.readedXElement.getSingleText("Payee");
//                            PayeeAcct	收款人账号	是	30	String
                                detail.payeeAct = tmpBkReader.readedXElement.getSingleText("PayeeAcct");
//                            PayeeBank	收款人开户行	是	50	String
                                detail.payeeBnkName = tmpBkReader.readedXElement.getSingleText("PayeeBank");
                                //收款人开户机构号
                                detail.payeeBranch = tmpBkReader.readedXElement.getSingleText("PayeeBranch");
//                            PayeeBankNo	收款人开户行行号	是	20	String
                                detail.payeeBnkCode = tmpBkReader.readedXElement.getSingleText("PayeeBankNo");
//                            Acpt	承兑人/承兑行	是	50	String
                                detail.accpName = tmpBkReader.readedXElement.getSingleText("Acpt");
//                            AcptAcct	承兑人账号	否	30	String
                                detail.accpAct = tmpBkReader.readedXElement.getSingleText("AcptAcct");
//                            AcptBankNo	承兑人开户行	是	30	String
                                detail.accpBnkCode = tmpBkReader.readedXElement.getSingleText("AcptBankNo");
//                            Conferno	交易合同号	是	30	String
                                detail.contractNo = tmpBkReader.readedXElement.getSingleText("Conferno");
//                            Isendo	是否可转让	是	4	String	EM00-可再转让 EM01-不得转让
                                detail.transFlag = "EM00".equals(tmpBkReader.readedXElement.getSingleText("Isendo")) ? "1" : "0";
//                            BillStatus	票据状态	是	20	String	直接返回中文
                                detail.billStatus = getBankConfig().getMapValueByKey("billStatuMapping", tmpBkReader.readedXElement.getSingleText("BillStatus"));
                                detail.reserved1 = tmpBkReader.readedXElement.getSingleText("respond1");
                                detail.reserved2 = tmpBkReader.readedXElement.getSingleText("respond2");
                                retAts.writeDetail(detail);
                            }
                        } else {
                            if (StringUtil.isNull(returnInfo)){
                                returnInfo=tmpBkReader.getFirstText("/transaction/ReturnMsg");
                            }
                            throw new Exception(returnInfo);
                        }

                    } else {
                        throw new Exception(tmpSbmsg.toString());
                    }
                } else {
                    throw new Exception(result[1]);
                }
            } while (start - 1 < total);
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsBatchInfoQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsBatchAddSubmit batchAddSubmit(ReqBatchAddSubmit query) throws Exception {
        RetAtsBatchAddSubmit retAts = new RetAtsBatchAddSubmit(query.reqHeader);
        retAts.respCode = "0";
        retAts.writeBefore();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.batchID = getBatchID();
            header.TransCode = "b2e003003";
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlWriter, header);
            xmlWriter.writeStartElement("TransContent");
            xmlWriter.writeStartElement("ReqData");
            //          ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
            xmlWriter.writeElementString("ClientPatchID", query.reqHeader.transSeqID);
            //出票人账号
            xmlWriter.writeElementString("AcNo", query.detail.drawAct);
            //承兑人
            xmlWriter.writeElementString("AcName2", query.detail.accpAct);
            xmlWriter.writeElementString("BankType", query.detail.accpSameBnk);
            xmlWriter.writeElementString("AcptBranch", query.detail.accpBranch);
            xmlWriter.writeElementString("AcptBankNo", query.detail.accpBnkCode);
            xmlWriter.writeElementString("AcptBankName", query.detail.accpBnkName);
            xmlWriter.writeElementString("AcNo2", query.detail.accpAct);
            xmlWriter.writeElementString("AcceptAgmtNo", query.detail.accpProtocolNo);
            String billType;
            if ("1".equals(query.detail.billType)) {
                billType = "AC01";
            } else {
                billType = "AC02";
            }
            String isenDo;
            if ("1".equals(query.detail.transFlag)) {
                isenDo = "EM00";
            } else {
                isenDo = "EM01";
            }
            //            BillType	票据类型	否	4	String	见数据字典
            xmlWriter.writeElementString("BillType", billType);
            xmlWriter.writeElementString("Isendo", isenDo);
            xmlWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlWriter.toXmlString(), header.TransCode);

            if (result[0].length() == 0) {
                StringBuilder tmpSbmsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbmsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        if (tmpBkReader.read("/Transaction/TransContent/RespData")) {
                            RetAtsBatchAddSubmitDetail detail = new RetAtsBatchAddSubmitDetail();
                            // BillId	批次号	是	20	String
                            detail.batchNo = tmpBkReader.readedXElement.getSingleText("BatchNo");
                            // BillNo	协议编号	是	30	String
                            detail.cecmmo = tmpBkReader.readedXElement.getSingleText("Cecmmo");
                            retAts.writeDetail(detail);
                        }
                    } else {
                        if (StringUtil.isNull(returnInfo)){
                            returnInfo=tmpBkReader.getFirstText("/transaction/ReturnMsg");
                        }
                        throw new Exception(returnInfo);
                    }

                } else {
                    throw new Exception(tmpSbmsg.toString());
                }
            } else {
                throw new Exception(result[1]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsBatchAddSubmit(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsBatchModSubmit batchModSubmit(ReqBatchModSubmit query) throws Exception {
        RetAtsBatchModSubmit retAts = new RetAtsBatchModSubmit(query.reqHeader);
        retAts.respCode = "0";
        retAts.writeBefore();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.batchID = getBatchID();
            header.TransCode = "b2e003004";
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlWriter, header);
            xmlWriter.writeStartElement("TransContent");
            xmlWriter.writeStartElement("ReqData");
            //ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
            xmlWriter.writeElementString("ClientPatchID", query.reqHeader.transSeqID);
            //出票人账号
            xmlWriter.writeElementString("AcNo", query.detail.drawAct);
            xmlWriter.writeElementString("BatchNo", query.detail.batchNo);
            //承兑人
            xmlWriter.writeElementString("AcName2", query.detail.accpAct);
            xmlWriter.writeElementString("BankType", query.detail.accpSameBnk);
            xmlWriter.writeElementString("AcptBranch", query.detail.accpBranch);
            xmlWriter.writeElementString("AcptBankNo", query.detail.accpBnkCode);
            xmlWriter.writeElementString("AcptBankName", query.detail.accpBnkName);
            xmlWriter.writeElementString("AcNo2", query.detail.accpAct);
            String billType;
            if ("1".equals(query.detail.billType)) {
                billType = "AC01";
            } else {
                billType = "AC02";
            }
            String isenDo;
            if ("1".equals(query.detail.transFlag)) {
                isenDo = "EM00";
            } else {
                isenDo = "EM01";
            }
            //            BillType	票据类型	否	4	String	见数据字典
            xmlWriter.writeElementString("BillType", billType);
            xmlWriter.writeElementString("Isendo", isenDo);
            xmlWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlWriter.toXmlString(), header.TransCode);

            if (result[0].length() == 0) {
                StringBuilder tmpSbmsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbmsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        if (tmpBkReader.read("/Transaction/TransContent/RespData")) {
                            retAts.respCode = "0";
                        }
                    } else {
                        if (StringUtil.isNull(returnInfo)){
                            returnInfo=tmpBkReader.getFirstText("/transaction/ReturnMsg");
                        }
                        throw new Exception(returnInfo);
                    }

                } else {
                    throw new Exception(tmpSbmsg.toString());
                }
            } else {
                throw new Exception(result[1]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsBatchModSubmit(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsBatchDelSubmit batchDelSubmit(ReqBatchDelSubmit query) throws Exception {
        RetAtsBatchDelSubmit retAts = new RetAtsBatchDelSubmit(query.reqHeader);
        retAts.respCode = "0";
        retAts.writeBefore();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.batchID = getBatchID();
            header.TransCode = "b2e003005";
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlWriter, header);
            xmlWriter.writeStartElement("TransContent");
            xmlWriter.writeStartElement("ReqData");
            //ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
            xmlWriter.writeElementString("ClientPatchID", query.reqHeader.transSeqID);
            //出票人账号
            xmlWriter.writeElementString("BatchNo", query.detail.batchNo);
            //承兑人
            xmlWriter.writeElementString("AcNo", query.detail.drawAct);
            xmlWriter.writeEndDocument();
            TimeUnit.SECONDS.sleep(5);
            String[] result = sendToBank_2(xmlWriter.toXmlString(), header.TransCode);

            if (result[0].length() == 0) {
                StringBuilder tmpSbmsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbmsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        if (tmpBkReader.read("/Transaction/TransContent/RespData")) {
                            retAts.respCode = "0";
                        }
                    } else {
                        if (StringUtil.isNull(returnInfo)){
                            returnInfo=tmpBkReader.getFirstText("/transaction/ReturnMsg");
                        }
                        throw new Exception(returnInfo);
                    }

                } else {
                    throw new Exception(tmpSbmsg.toString());
                }
            } else {
                throw new Exception(result[1]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsBatchDelSubmit(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsBatchSubmitApply batchSubmitApply(ReqBatchSubmitApply query) throws Exception {
        RetAtsBatchSubmitApply retAts = new RetAtsBatchSubmitApply(query.reqHeader);
        retAts.respCode = "0";
        retAts.writeBefore();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.batchID = getBatchID();
            header.TransCode = "b2e003006";
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlWriter, header);
            xmlWriter.writeStartElement("TransContent");
            xmlWriter.writeStartElement("ReqData");
            //ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
            xmlWriter.writeElementString("ClientPatchID",query.reqHeader.transSeqID);
            //出票人账号
            xmlWriter.writeElementString("BatchNo", query.detail.batchNo);
            //承兑人
            xmlWriter.writeElementString("AcNo", query.detail.drawAct);
            xmlWriter.writeElementString("Mobile", query.detail.accpTel);
            xmlWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlWriter.toXmlString(), header.TransCode);

            if (result[0].length() == 0) {
                StringBuilder tmpSbmsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbmsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        if (tmpBkReader.read("/Transaction/TransContent/RespData")) {
                            retAts.respCode = "0";
                        }
                    } else {
                        if (StringUtil.isNull(returnInfo)){
                            returnInfo=tmpBkReader.getFirstText("/transaction/ReturnMsg");
                        }
                        throw new Exception(returnInfo);
                    }

                } else {
                    throw new Exception(tmpSbmsg.toString());
                }
            } else {
                throw new Exception(result[1]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsBatchSubmitApply(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsBatchSubmitCancel batchSubmitCancel(ReqBatchSubmitCancel query) throws Exception {
        RetAtsBatchSubmitCancel retAts = new RetAtsBatchSubmitCancel(query.reqHeader);
        retAts.respCode = "0";
        retAts.writeBefore();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.batchID = getBatchID();
            header.TransCode = "b2e003007";
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlWriter, header);
            xmlWriter.writeStartElement("TransContent");
            xmlWriter.writeStartElement("ReqData");
            //ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
            xmlWriter.writeElementString("ClientPatchID", query.reqHeader.transSeqID);
            //出票人账号
            xmlWriter.writeElementString("BatchNo", query.detail.batchNo);
            //承兑人
            xmlWriter.writeElementString("AcNo", query.detail.drawAct);
            xmlWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlWriter.toXmlString(), header.TransCode);
            if (result[0].length() == 0) {
                StringBuilder tmpSbmsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbmsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        if (tmpBkReader.read("/Transaction/TransContent/RespData")) {
                            retAts.respCode = "0";
                        }
                    } else {
                        if (StringUtil.isNull(returnInfo)){
                            returnInfo=tmpBkReader.getFirstText("/transaction/ReturnMsg");
                        }
                        throw new Exception(returnInfo);
                    }

                } else {
                    throw new Exception(tmpSbmsg.toString());
                }
            } else {
                throw new Exception(result[1]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsBatchSubmitCancel(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsBillAddSubmit billAddSubmit(ReqBillAddSubmit query) throws Exception {
        RetAtsBillAddSubmit retAts = new RetAtsBillAddSubmit(query.reqHeader);
        retAts.respCode = "0";
        retAts.writeBefore();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.batchID = getBatchID();
            header.TransCode = "b2e003008";
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlWriter, header);
            xmlWriter.writeStartElement("TransContent");
            xmlWriter.writeStartElement("ReqData");
            //          ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
            xmlWriter.writeElementString("ClientPatchID", query.reqHeader.transSeqID);
            //出票人账号
            xmlWriter.writeElementString("BatchNo", query.detail.batchNo);
            xmlWriter.writeElementString("AcNo", query.detail.drawAct);
            //承兑人
            xmlWriter.writeElementString("Amount", query.detail.billAmt);
            xmlWriter.writeElementString("AcptDate", query.detail.issDate);
            xmlWriter.writeElementString("DueDate", query.detail.dueDate);
            xmlWriter.writeElementString("AcName2", query.detail.payeeName);
            xmlWriter.writeElementString("BankType", query.detail.payeeSameBnk);
            xmlWriter.writeElementString("PayeeBankName", query.detail.payeeBnkName);
            xmlWriter.writeElementString("PayeeBankNo", query.detail.payeeBnkCode);
            xmlWriter.writeElementString("AcNo2", query.detail.payeeAct);
            xmlWriter.writeElementString("ConfernNo", query.detail.contractNo);
            xmlWriter.writeElementString("Remark", query.detail.remark);
            String isenDo;
            if ("1".equals(query.detail.transFlag)) {
                isenDo = "EM00";
            } else {
                isenDo = "EM01";
            }
            xmlWriter.writeElementString("Isendo", isenDo);
            xmlWriter.writeEndDocument();
            TimeUnit.SECONDS.sleep(5);
            String[] result = sendToBank_2(xmlWriter.toXmlString(), header.TransCode);

            if (result[0].length() == 0) {
                StringBuilder tmpSbmsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbmsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        if (tmpBkReader.read("/Transaction/TransContent/RespData")) {
                            retAts.billId = tmpBkReader.readedXElement.getSingleText("BillId");
                            retAts.writeDetail();
                        }
                    } else {
                        if (StringUtil.isNull(returnInfo)){
                            returnInfo=tmpBkReader.getFirstText("/transaction/ReturnMsg");
                        }
                        throw new Exception(returnInfo);
                    }

                } else {
                    throw new Exception(tmpSbmsg.toString());
                }
            } else {
                throw new Exception(result[1]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsBillAddSubmit(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsBillAddCancelSubmit billAddCancelSubmit(ReqBillAddCancelSubmit query) throws Exception {
        RetAtsBillAddCancelSubmit retAts = new RetAtsBillAddCancelSubmit(query.reqHeader);
        retAts.respCode = "0";
        retAts.writeBefore();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CEB01Header header = new CEB01Header();
            header.batchID = getBatchID();
            header.TransCode = "b2e003009";
            header.JnlDateTime = new Date();
            writeCommonHeader_2(xmlWriter, header);
            xmlWriter.writeStartElement("TransContent");
            xmlWriter.writeStartElement("ReqData");
            //ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
            xmlWriter.writeElementString("ClientPatchID", query.reqHeader.transSeqID);
            //出票人账号
            xmlWriter.writeElementString("BillId", query.detail.billId);
            //承兑人
            xmlWriter.writeElementString("AcNo", query.detail.drawAct);
            xmlWriter.writeEndDocument();
            String[] result = sendToBank_2(xmlWriter.toXmlString(), header.TransCode);

            if (result[0].length() == 0) {
                StringBuilder tmpSbmsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbmsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String returnInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if ("0000".equals(returnCode)) {
                        if (tmpBkReader.read("/Transaction/TransContent/RespData")) {
                            retAts.respCode = "0";
                        }
                    } else {
                        if (StringUtil.isNull(returnInfo)){
                            returnInfo=tmpBkReader.getFirstText("/transaction/ReturnMsg");
                        }
                        throw new Exception(returnInfo);
                    }

                } else {
                    throw new Exception(tmpSbmsg.toString());
                }
            } else {
                throw new Exception(result[1]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsBillAddCancelSubmit(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsBatOpenBillSubmit BatOpenBillSubmit(ReqBatOpenBillSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsBatOpenBillSubmitQry BatOpenBillSubmitQry(ReqBatOpenBillSubmitQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsOrgQry orgQry(ReqOrgQry query) throws Exception {
        RetAtsOrgQry retAts = new RetAtsOrgQry(query.reqHeader);
        retAts.respCode = "0";
        retAts.writeBefore();
        query.readRecord();
        int start = 1;
        int total = 0;
        try {
            do {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                CEB01Header header = new CEB01Header();
                header.batchID = getBatchID();
                header.TransCode = "b2e003030";
                header.JnlDateTime = new Date();
                writeCommonHeader_2(xmlWriter, header);
                xmlWriter.writeStartElement("TransContent");
                xmlWriter.writeStartElement("ReqData");
                //          ClientPatchID	客户端批内序号/客户交易号	是	50	String	参考说明
                xmlWriter.writeElementString("ClientPatchID", getBatchID());
                //            AcNo	账号	是	30	String	支持授权账户，授权关系由票据系统维护，如上送户名则银企通判别户名是否正确
                xmlWriter.writeElementString("OrganType", query.detail.organType);
                //            StartNum	起始笔数	是	7	String
                xmlWriter.writeElementString("StartNum", "" + start);
                //            QueryNum	查询笔数	是	7	String
                xmlWriter.writeElementString("QueryNum", "99");
                xmlWriter.writeEndDocument();
                TimeUnit.SECONDS.sleep(5);
                String[] result = sendToBank_2(xmlWriter.toXmlString(), header.TransCode);
                if (result[0].length() == 0) {
                    StringBuilder tmpSbmsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbmsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                        String returnInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                        String returnNum = tmpBkReader.getFirstText("/Transaction/TransContent/TotalNum");
                        if ("0000".equals(returnCode)) {
                            if (total == 0) {
                                total = Integer.valueOf(returnNum);
                            }
                            while (tmpBkReader.read("/Transaction/TransContent/BatchRespList/RespData")) {
                                start++;
                                RetAtsOrgQryDetail detail = new RetAtsOrgQryDetail();
                                detail.organId = tmpBkReader.readedXElement.getSingleText("OrganId");
                                detail.organName = tmpBkReader.readedXElement.getSingleText("OrganName");
                                detail.reserved1 = tmpBkReader.readedXElement.getSingleText("respond1");
                                detail.reserved2 = tmpBkReader.readedXElement.getSingleText("respond2");
                                retAts.writeDetail(detail);
                            }
                        } else {
                            if (StringUtil.isNull(returnInfo)){
                                returnInfo=tmpBkReader.getFirstText("/transaction/ReturnMsg");
                            }
                            throw new Exception(returnInfo);
                        }

                    } else {
                        throw new Exception(tmpSbmsg.toString());
                    }
                } else {
                    throw new Exception(result[1]);
                }
            } while (start < total);
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsOrgQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }

        retAts.writeEnd();
        return retAts;
    }
}
